package com.leetroll.media.service;

import com.leetroll.api.service.FileService;
import com.leetroll.common.entity.File;
import com.leetroll.common.entity.Item;
import com.leetroll.common.utils.AliyunOSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public  class CleanService {

    private final FileService fileService;

    @Autowired
    public CleanService(FileService fileService) {
        this.fileService = fileService;
    }

    public void clean (List<Item> waitingDelItems) {
        for (Item delItem : waitingDelItems) {
            List<File> files = fileService.listByRescId(delItem.getId());
            String path = "item/" + delItem.getId() + "/";
            AliyunOSSUtil.deleteFolder(path);
            for (File file : files) {
                file.deleteById();
            }
            delItem.deleteById();
        }
        log.info("clean job finished: {} in total", waitingDelItems.size());
    }
}
